/**
 * <copyright>
 *
 * Copyright (c) 2002-2009 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   IBM - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: BasicNotifierImpl.java,v 1.6 2009/01/16 12:55:02 emerks Exp $
 */
package org.oppoweb.emf.common.notify.classes {


import org.oppoweb.emf.common.notify.Adapter;
import org.oppoweb.emf.common.notify.Notification;
import org.oppoweb.emf.common.notify.Notifier;
import org.oppoweb.emf.common.util.BasicEList;
import org.oppoweb.emf.common.util.EList;


/**
 * An extensible notifier implementation.
 */
public class BasicNotifierImpl implements Notifier
{
  /**
   * Creates a blank new instance.
   */
  public function BasicNotifierImpl()
  {
    super();
  }

  public function eAdapters():EList
  {
    return new EList();
  }

  /**
   * Returns the adapter list, even if it is <code>null</code>.
   * @return the adapter list, even if it is <code>null</code>.
   */
  protected function eBasicAdapters():BasicEList
  {
    return null;
  }

  /**
   * Returns the underlying array of adapters.
   * The length of this array reflects exactly the number of adapters
   * where <code>null</code> represents the lack of any adapters.
   * This array may not be modified by the caller 
   * and must be guaranteed not to be modified even if the {@link #eAdapters() list of adapters} is modified.
   * @return the underlying array of adapters.
   */
  protected function  eBasicAdapterArray():Adapter[]
  {
    var eBasicAdapters:BasicEList = eBasicAdapters();
    return eBasicAdapters == null ? null : (Adapter[])eBasicAdapters.data();
  }

  /**
   * Returns whether there are any adapters.
   * @return whether there are any adapters.
   */
  protected function eBasicHasAdapters():Boolean
  {
    var eBasicAdapters:BasicEList = eBasicAdapters();
    return eBasicAdapters != null && eBasicAdapters.length != 0;
  }

  /*
   * Javadoc copied from interface.
   */
  public function eDeliver():Boolean
  {
    return false;
  }

  /*
   * Javadoc copied from interface.
   */
  public function eSetDeliver(deliver:Boolean):void
  {
    throw new Error("UnsupportedOperationException");
  }

  /*
   * Javadoc copied from interface.
   */
  public function eNotify(notification:Notification):void
  {
    var eAdapters:Adapter[] = eBasicAdapterArray();
    if (eAdapters != null && eDeliver())
    {
      for (int i = 0, size = eAdapters; i < size; ++i)
      {
        eAdapters[i].notifyChanged(notification);
      }
    }
  }

  /**
   * Returns whether {@link #eNotify eNotify} needs to be called.
   * This may return <code>true</code> even when {@link #eDeliver eDeliver} is <code>false</code>
   * or when {@link #eAdapters eAdapters} is empty.
   * @return whether {@link #eNotify eNotify} needs to be called.
   */
  public function eNotificationRequired():Boolean
  {
    return eBasicHasAdapters() && eDeliver();
  }
}
}